package com.chenzhou.product_sys.controller;

import com.chenzhou.product_sys.entity.GoodsEntity;
import com.chenzhou.product_sys.entity.GoodsTypeEntity;
import com.chenzhou.product_sys.service.GoodsService;
import com.chenzhou.product_sys.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsTypeService goodsTypeService;

    @RequestMapping("/queryAllGoods")
    public String queryAllGoods(HttpServletRequest request){
        List<GoodsEntity> list = goodsService.queryAllGoods();
        request.setAttribute("glist",list);
        request.setAttribute("menu","goods");
        return "goods/show_goods";
    }

    @RequestMapping("/queryGoodsOne")
    public String queryGoodsOne(HttpServletRequest request,Integer id){
        GoodsEntity goodsEntity = goodsService.queryGoodsByID(id);
        List<GoodsTypeEntity> list = goodsTypeService.queryAllGoodsType();
        request.setAttribute("goods",goodsEntity);
        request.setAttribute("gtlist",list);
        request.setAttribute("menu","goods");
        return "goods/modify_Goods";
    }

    @RequestMapping("/goto_addGoods")
    public String goto_Add_Goods(HttpServletRequest request){
        List<GoodsTypeEntity> list = goodsTypeService.queryAllGoodsType();
        request.setAttribute("gtlist",list);
        return "goods/add_goods";
    }

    @RequestMapping("/add_goods")
    public String add_Goods(HttpServletRequest request, GoodsEntity goodsEntity){
        int a = goodsService.add_Goods(goodsEntity);
        if(a>0){
            return  "redirect:/goods/queryAllGoods";//防止重复提交
        }else{
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/modify_goods")
    public String modify_Goods(HttpServletRequest request,  GoodsEntity goodsEntity){
        int a = goodsService.modify_Goods(goodsEntity);
        if(a>0){
            return  "redirect:/goods/queryAllGoods"; //防止重复提交
        }else{
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/delete_goods")
    public String delete_Goods(HttpServletRequest request, Integer id){
        int a = goodsService.delete_Goods(id);
        if(a>0){
            return  "redirect:/goods/queryAllGoods"; //防止重复提交
        }else{
            return "forward:/error.jsp";
        }
    }
}
